<?php
/**
 * Created by PhpStorm.
 * User: ysfl
 * Date: 16-2-28
 * Time: 上午5:32
 */

namespace modules\admin;

use Cross\MVC\Module;

class ClentsModule extends Module
{
    /**
     * 企业客户表
     *
     * @var string
     */
    protected $tables = 'xrk_ec';

    /**
     * 会员客户表
     *
     * @var string
     */
    protected $vip = 'xrk_user';

    /**
     * 添加企业客户
     *
     * @param $data
     * @return text
     */
    function Clents_Company_Add($data)
    {
        //检查表单是否为空
        foreach ($data as $key => & $val) {

            if (empty($val)) {

                switch($key)
                {
                    case 'name':
                        $name = "公司名称";
                        break;
                    case 'f_mobile':
                        $name = "负责人联系电话";
                        break;
                    case 'z_mobile':
                        $name = "公司座机";
                        break;
                    case 'c_mobile':
                        $name = "财务电话";
                        break;
                    case 'addr':
                        $name = "公司地址";
                        break;
                    case 'o_name':
                        $name = "操作员";
                        break;
                    case 'o_time':
                        $name = "添加时间";
                        break;
                    default:
                        $name = "未知错误";
                        break;
                }
                return '<div class="alert alert-danger" role="alert">请输入：'.$name.'</div>';
                unset($data[$key]);
            }
        }

        //检测企业客户是否存在
        if ($this->link->get($this->tables, 'name', array('name' => $data['name']))){
            return '<div class="alert alert-danger" role="alert">这个企业客户已经存在！</div>';
        }

        //数据写入数据库
        if ($this->link->add($this->tables, $data)){
            return '<div class="alert alert-success" role="alert">企业客户：<b style="color:green">'.$data['name'].'</b> 添加成功！</div>';
        }
        else{
            return '<div class="alert alert-danger" role="alert">添加失败，请联系管理员</div>';
        }
    }

    /**
     * 查询企业客户
     * @return array
     */
    function Clents_Company_Query()
    {
        //查询并返回结果
        return $this->link->getAll($this->tables, '*');
    }

    /**
     * 查询企业客户当前未付款
     * @return array
     */
    function Clents_Company_Unpaid()
    {
        //查询并返回结果
        return $this->link->select('g_id,sum(price) as price,mprice,start')
            ->from('xrk_wastebook')
            ->where(array('start' => 0,'mprice' => '未付'))
            ->groupBy('g_id')
            ->stmt()
            ->fetchAll(\PDO::FETCH_ASSOC);
    }

    /**
     * 查询企业客户年度消费额
     * @return array
     */
    function Clents_Company_Consumer_Sum()
    {
        //查询并返回结果
        return $this->link->select('g_id,sum(price) as price,start')
            ->from('xrk_wastebook')
            ->where(array('start' => 1))
            ->groupBy('g_id')
            ->stmt()
            ->fetchAll(\PDO::FETCH_ASSOC);
    }

    /**
     * 查询会员客户
     * @return array
     */
    function Clents_Vip_Query()
    {
        //查询并返回结果
        return $this->link->select('name,uname,mobile,addr,free_num')
            ->from($this->vip)
            ->stmt()
            ->fetchAll(\PDO::FETCH_ASSOC);
    }
}